/* eslint-disable no-undef */
import {
  getCurrentVersion,
  updateTab,
  getTaxConfig,
  getNationData
} from './utils.js';
import { caishui } from '../lib/caishui.js';

// 报税信息
window.baseInfo = {};
window.taxGovInfo = {};
// 开票信息
window.invoiceInfo = {};
// 零申报信息
window.zeroInfo = {
  isOpenZeroReport: false,
  data: [],
  currentZeroInfo: {},
  taxConfig: {},
  workTab: {},
  currentIndex: 0
};
// 同步申报明细表
window.asyncTableDetailInfo = {
  isStart: false,
  tableDetailList: [],
};

window.bgMethods = {
  fillTax: function (param) {
    let taxConfig = window.baseInfo.taxConfig;
    param.cityName = taxConfig ? taxConfig[0].name : '重庆';
    window.baseInfo.popupParams = param; // 存储popup传递参数
    window.baseInfo.companyInfo.useOneClickNum += 1;
    getNationData({ ...param, taxType: param.type, baseInfo: window.baseInfo  });
  },
  jump: function (type) {
    let companyInfo = baseInfo.companyInfo;
    if (!companyInfo) {
      caishui.notifications('没有获取到城市信息，请通过一键报税进入');
    } else {
      let taxConfig = getTaxConfig(companyInfo);
      let info = taxConfig.find(item => {
        return item.type === type;
      });
      if (info && info.url) {
        chrome.tabs.create({
          url: info.url
        });
      }
    }
  },
  updateTab,
  /*
   * 报税完成
   * */
  taxCompletion: function () {
    let port = null;
    let manifest = chrome.runtime.getManifest();
    let version = manifest.version;
    let companyInfo = window.baseInfo.companyInfo;
    if (!companyInfo) {
      caishui.notifications('请在税局页面点报税完成');
    }
    let taxConfig = getTaxConfig(companyInfo);
    taxConfig = taxConfig ? taxConfig[0] : [{}];
    let isSupport = taxConfig.isSupport || false;
    let useOneClickNum = companyInfo.useOneClickNum;
    if (baseInfo.saasTab) {
      port = caishui.tabsConnect(baseInfo.saasTab.tab.id, {
        name: 'toSaas'
      });
      port.postMessage({
        goType: 'taxCompletion',
        data: {
          version,
          countyName: taxConfig.name,
          isSupport,
          useOneClickNum
        },
        event: 'taxCompletion'
      });
    } else {
      caishui.getCurrentTabId(tid => {
        port = caishui.tabsConnect(tid, { name: 'toInject' });
        port.postMessage({
          goType: 'taxCompletion',
          data: {
            version,
            countyName: companyInfo.countyName,
            isSupport,
            useOneClickNum
          },
          event: 'taxCompletion'
        });
      });
    }
  },
  getCurrentVersion
};
